package cn.tsmchina.one.dao.activity;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.tsmchina.one.dao.BaseDaoSupport;
import cn.tsmchina.one.domain.activity.Activity;
import cn.tsmchina.one.util.C;

@Repository
public class ActivityDaoImpl extends BaseDaoSupport<Activity> implements ActivityDao
{
	@Override
	public int addActivity(Activity activity)
	{
		return (Integer) super.add(activity);
	}
	
	@Override
	public void deleteById(int id) {
		super.delete(id);
	}
	
	@Override
	public List<Activity> activities(int page, long cid)
	{
		String hql = "from Activity a where a.customerId=? order by a.id desc";
		lists(page,C.Paging.SIZE);
		return lists(hql,page,C.Paging.SIZE, new Object[]{cid});
	}

	@Override
	public int cancelActivity(int id)
	{
		String hql = "update from Activity a set a.delflag = ? where id = ?";
		return executeUpdate(hql, true,id);
	}

	@Override
	public void updateActivity(Activity activity)
	{
		super.update(activity);
	}

	@Override
	public int total(long cid)
	{
		String hql = "select count(*) from Activity a where a.customerId=? and a.delflag = 0";
		return count(hql, cid);
	}

	@Override
	public int check(String name, Object value)
	{
		String hql = "select count(*) from Activity a a."+name+" = ?";
		return executeUpdate(hql, value);
	}

	@Override
	public Activity findActivity(int id)
	{
		return findById(id);
	}

	@Override
	public Activity getActivity(int id)
	{
		return super.findById(id);
	}

	@Override
	public List<Activity> getActivityNotice()
	{
		// TODO Auto-generated method stub
		String hql ="from Activity a where a.startDate > date(now()) and a.delflag = 0 order by a.createDate desc ";
		return super.lists(hql);
	}

	@Override
	public List<Activity> getActivityShare()
	{
		String hql = "from Activity a where a.endDate < date(now()) and a.delflag = 0 order by a.createDate desc";
		return super.lists(hql);
	}

	@Override
	public void addJoin(String json,int id)
	{
		String hql = "update from Activity a set a.joinUserId = ? where a.id = ?";
		super.executeUpdate(hql, json,id);
	}

	@Override
	public void addPraise(String parseJson, int activityId)
	{
		String hql = "update from Activity a set a.praiseUserId = ? where a.id = ?";
		executeUpdate(hql, parseJson,activityId);
	}

	@Override
	public int redoActivity(int id) {
		String hql = "update from Activity a set a.delflag = ? where id = ?";
		return executeUpdate(hql,false,id);
	}
	
}
